package com.fingard.dsp.bank.directbank.sdebank01;


import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillTrans;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.*;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.detail.*;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.sql.Connection;
import java.util.Date;
import java.util.UUID;

/**
 * @author Parker on 2020-01-19.
 * @version 1.0
 */
public class SDEBANK01ElecBillTrans extends SDEBANK01Base implements IElecBillTrans {
    //应答处理 ok
    @Override
    public RetAtsB003BillRespSubmit D_B003_BillResp_Submit(ReqB003BillRespSubmit query) throws Exception {
        RetAtsB003BillRespSubmit retAts = new RetAtsB003BillRespSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EAcceptSign";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            RetAtsB003BillRespSubmitDetail detail = new RetAtsB003BillRespSubmitDetail();
            try {
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//账号
//                RespType 回复类型 Char(4) F
//                0001：承兑回复
//                0002：出票回复
//                0003：背书回复
//                0004：贴现回复
//                0005：贴现赎回回复
//                0006：转贴现回复
//                0007：转贴现赎回回复
//                0009：再贴现赎回回复
//                0010：央行卖票回复
//                0011：保证回复
//                0012：质押回复
//                0013：质押解除回复
//                0014：提示付款回复
//                0015：逾期提示付款回复
//                0016：同意清偿回复
//                网银可以回复：0001、0002、0003、0005、0011、0012、0013、0014、0015、0016
//                CP+CBD可以回复：
//                0001、0004、0006、0007、0009、0010、0011、0012、0013、0014、0015、0016
                String bkType;
                switch (Integer.valueOf(query.detail.respType)) {
                    //1-承兑应答 2-收票应答 3-背书转让应答 4-回购式贴现赎回应答 5-质押解除应答 6-提示付款应答 7-保证应答 8-同意清偿应 9-质押签收
                    case 1: bkType = "0001";break;
                    case 2: bkType = "0002";break;
                    case 3: bkType = "0003";break;
                    case 4: bkType = "0005";break;
                    case 5: bkType = "0013";break;
                    case 6:
                        if ("1".equals(query.detail.proPayType)) {
                            bkType = "0014";
                        } else {
                            bkType = "0015";
                        }
                        break;
                    case 7: bkType = "0011";break;
                    case 8: bkType = "0016";break;
                    case 9: bkType = "0012";break;
                    default: throw new Exception("未知的签收类型：" + query.detail.respType);
                }
                xmlWriter.writeElementString("RespType", bkType);//回复类型
                //回复标记 SU00同意签收  SU01拒绝签收
                xmlWriter.writeElementString("AccptrReplyMk", getRespFlag(query.detail.respFlag));
                //承兑人信用等级
                xmlWriter.writeElementString("AccptrCdtRatgs", query.detail.accpCredit);
                //承兑人评级机构
                xmlWriter.writeElementString("AccptrCdtRatgAgcy", query.detail.accpGrade);
                //承兑人评级到期日
                xmlWriter.writeElementString("AccptrCdtRatgDueDt", query.detail.accpGradeDue);
                //保证人地址
                xmlWriter.writeElementString("GuarntrAddress", "");
                //协议编号
                xmlWriter.writeElementString("SAccepAgreeNo", query.detail.protocolNo);
                //回复人备注
                xmlWriter.writeElementString("Remrk", query.detail.memo+splitToString+query.detail.reqSeqID);
                //代码
                if ("6".equals(query.detail.respType) && "2".equals(query.detail.respFlag)) {
                    if (StringHelper.isNullOrEmpty(query.detail.unPayCode)) {
                        throw new Exception("提示付款拒签时，拒付理由不能为空!");
                    }
                    if ("99".equals(query.detail.unPayCode)) {
                        xmlWriter.writeElementString("DishonorCode", "DC09");
                    } else {
                        xmlWriter.writeElementString("DishonorCode", "DC0" + (Integer.valueOf(query.detail.unPayCode) - 1));
                    }
                } else {
                    xmlWriter.writeElementString("DishonorCode", "");
                }
                //拒付备注信息
                xmlWriter.writeElementString("DishonorRsn", query.detail.unPayMemo);
                xmlWriter.writeStartElement("DraftInfo");
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("draftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("isseAmt", query.detail.billAmt);//电子票据金额
                String[] ecdsInfo = query.detail.busiNo.split("_");
                if (StringHelper.hasAnyChar(query.detail.busiNo)) {
                    //                20 BkOldPlatDate 原ECDS平台日期 Char(8) F 　
                    xmlWriter.writeElementString("EcdsDate", ecdsInfo[0]);
//                21 BkOldSeq 原ECDS平台流水 Int F 　
                    xmlWriter.writeElementString("EcdsSeq", ecdsInfo[1]);
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "原ECDS平台流水异常。请确认申请结果查询是否返回EcdsDate和EcdsSeq的值";
                    WriteBankLogLn("原ECDS平台流水异常。请确认申请结果查询是否返回EcdsDate和EcdsSeq的值");
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                                if (StringHelper.hasAnyChar(billNo) && billNo.equals(query.detail.billNo)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                    detail.reqSeqID = query.detail.reqSeqID;
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                    detail.payInfoCode = detail.transState;
                                    break;
                                } else {
                                    detail.respCode = "-1";
                                    detail.respInfo = "查无记录";
                                }
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //出票 ok
    @Override
    public RetAtsB101BillApplySubmit D_B101_BillResp_Submit(ReqB101BillApplySubmit query) throws Exception {
        RetAtsB101BillApplySubmit retAts = new RetAtsB101BillApplySubmit(query.reqHeader);
        RetAtsB101BillApplySubmitDetail detail = new RetAtsB101BillApplySubmitDetail();
        retAts.writeBeforeDetail();
        //赋值通用参数
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EIssueRegister";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入SDEBANK01通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            //写入发送给银行报文体
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("DraftType", getBillType(query.detail.billType));//票据种类
            xmlWriter.writeElementString("DrwrCredit", query.detail.creditLv);//出票人信用等级
            xmlWriter.writeElementString("DrwrCreditAgency", query.detail.gradeAgent);//出票人评级机构
            xmlWriter.writeElementString("DrwrCreditDueDt", DateHelper.convertDateTimetoD8(query.detail.gradeDueDate));//出票人评级到期日
            xmlWriter.writeElementString("IsseAmt", query.detail.billAmt);//票据金额
            xmlWriter.writeElementString("IsseDt", DateHelper.convertDateTimetoD8(query.detail.issDate));//出票日期
            xmlWriter.writeElementString("DueDt", DateHelper.convertDateTimetoD8(query.detail.dueDate));//到期日
            xmlWriter.writeElementString("BanEndrsmtMk", getTransFlag(query.detail.transFlag));//不得转让标记
            xmlWriter.writeElementString("DrwrAcctId", query.detail.drawAct);//出票人账号
            xmlWriter.writeElementString("AccptrNm", query.detail.accpName);//承兑人名称
            xmlWriter.writeElementString("AccptrId", query.detail.accpAct);//承兑人账号
            xmlWriter.writeElementString("AccptrSvcr", query.detail.accpBnkCode);//承兑人开户行行号
            xmlWriter.writeElementString("PyeeNm", query.detail.payeeName);//收款人名称
            xmlWriter.writeElementString("PyeeAcctId", query.detail.payeeAct);//收款人账号
            xmlWriter.writeElementString("PyeeAcctSvcr", query.detail.payeeBnkCode);//收款人开户行行号
            xmlWriter.writeElementString("PyeeAcctSvcrName", query.detail.payeeBnkName);//收款人开户行名
            xmlWriter.writeElementString("Remrk", query.detail.memo+splitToString+query.detail.reqSeqID);//出票人备注
            xmlWriter.writeElementString("DraftJnlNo", query.detail.reqSeqID);//出票流水号
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            // 发送给银行获得应答报文
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
            if (tmpStrRet[0].length() == 0) {// 成功响应
                StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                if (tmpBkReader != null) {// 返回报文的内容不为空
                    String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                    if ("000000".equals(IBSReturnCode)) {
                        String draftNb = tmpBkReader.getFirstText("/Message/Body/DraftNb");//电子票据号
                        detail.respCode = "0";
                        detail.respInfo = IBSReturnMsg;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = IBSReturnCode;
                        detail.payInfoCode = detail.transState;
                        if(StringHelper.hasAnyChar(draftNb)){
                            //记录ats支付流水号对应的电子票据号，供查询状态接口使用
                            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                            Connection conn = FGBiz.getLocalDbConn();//取本地文件数据库连接
                            try {
                                //批号
                                JDBCHelper.update(conn, INSERTSEQONE, new Object[]{uuid, query.reqHeader.bankCode, Format.Date8Format.format(new Date()), query.reqHeader.reqSeqID, draftNb});
                            } catch (Exception ex) {
                                WriteBankLogLn("电子票据号插入数据库失败");
                            }finally {
                                if(conn != null){
                                    conn.close();
                                }
                            }
                        }
                    } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                        detail.respCode = "0";
                        detail.respInfo = IBSReturnMsg;
                        detail.transState = IBSReturnCode;
                        detail.payInfoCode = detail.transState;
                        detail.reqSeqID=query.detail.reqSeqID;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpStrRet[1];
            }
            retAts.writeDetail(detail);
        }
        retAts.writeAfter();
        return retAts;
    }

    //提示承兑申请 ok
    @Override
    public RetAtsB102BillAccpSubmit D_B102_BillResp_Submit(ReqB102BillAccpSubmit query) throws Exception {
        RetAtsB102BillAccpSubmit retAts = new RetAtsB102BillAccpSubmit(query.reqHeader);
        RetAtsB102BillAccpSubmitDetail detail = new RetAtsB102BillAccpSubmitDetail();
        retAts.writeBeforeDetail();
        //赋值通用参数
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EAccept";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入SDEBANK01通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            //写入发送给银行报文体
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("SUnconPayComm", "CC00");//到期无条件支付委托
            xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//出票人账号
            xmlWriter.writeElementString("ContractNb", query.detail.contractNo);//交易合同编号
            xmlWriter.writeElementString("InvoiceNb", query.detail.invoiceNo);//发票号码
            xmlWriter.writeElementString("DrwrRemrk", query.detail.memo+splitToString+query.detail.reqSeqID);//出票人备注
            xmlWriter.writeStartElement("DraftInfo");
            xmlWriter.writeStartElement("Map");
            xmlWriter.writeElementString("draftNb", query.detail.billNo);//电子票据号码
            xmlWriter.writeElementString("isseAmt", query.detail.billAmt);//电子票据金额
            xmlWriter.writeEndDocument();

            // 发送给银行获得应答报文
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);

            if (tmpStrRet[0].length() == 0) {// 成功响应
                StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                if (tmpBkReader != null) {// 返回报文的内容不为空
                    String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                    if ("000000".equals(IBSReturnCode)) {
                        while (tmpBkReader.read("/Message/Body/List/Map")) {
                            String billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                            if (StringHelper.hasAnyChar(billNo) && billNo.equals(query.detail.billNo)) {
                                detail.respCode = "0";
                                detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                detail.reqSeqID = query.detail.reqSeqID;
                                detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                detail.payInfoCode = detail.transState;
                                break;
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = "查无记录";
                            }
                        }
                    } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                        detail.respCode = "0";
                        detail.respInfo = IBSReturnMsg;
                        detail.transState = IBSReturnCode;
                        detail.payInfoCode = detail.transState;
                        detail.reqSeqID=query.detail.reqSeqID;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpStrRet[1];
            }
            retAts.writeDetail(detail);
        }
        retAts.writeAfter();
        return retAts;
    }

    //撤票 ok
    @Override
    public RetAtsB103BillRetSubmit D_B103_BillResp_Submit(ReqB103BillRetSubmit query) throws Exception {
        RetAtsB103BillRetSubmit retAts = new RetAtsB103BillRetSubmit(query.reqHeader);
        RetAtsB103BillRetSubmitDetail detail = new RetAtsB103BillRetSubmitDetail();
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EIssueDestruction";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号

            try {
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter, bankHeader);
                xmlTextWriter.writeStartElement("Body");
                //签约电票的账号
                xmlTextWriter.writeElementString("EnterpriseAccount", query.detail.actNum);
                xmlTextWriter.writeStartElement("DraftInfo");
                xmlTextWriter.writeStartElement("Map");
                //电子票据号码
                xmlTextWriter.writeElementString("draftNb", query.detail.billNo);
                //票据金额
                xmlTextWriter.writeElementString("isseAmt", query.detail.billAmt);
                xmlTextWriter.writeEndDocument();
                // 发送给银行获得应答报文
                String[] result = sendToBank(xmlTextWriter.toXmlString(), bankHeader);

                if (result[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String draftNb = tmpBkReader.readedXElement.getSingleText("draftNb");
                                if (StringHelper.hasAnyChar(draftNb) && query.detail.billNo.equals(draftNb)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                    detail.reqSeqID = query.detail.reqSeqID;
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                    detail.payInfoCode = detail.transState;
                                    break;
                                } else {
                                    detail.respCode = "-1";
                                    detail.respInfo = "未返回数据";
                                }
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //收票 ok
    @Override
    public RetAtsB104BillReceSubmit D_B104_BillResp_Submit(ReqB104BillReceSubmit query) throws Exception {
        RetAtsB104BillReceSubmit retAts = new RetAtsB104BillReceSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EIssuanceRequest";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
            RetAtsB104BillReceSubmitDetail detail = new RetAtsB104BillReceSubmitDetail();
            try {
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter, bankHeader);
                xmlTextWriter.writeStartElement("Body");
                //签约电票的账号
                xmlTextWriter.writeElementString("EnterpriseAccount", query.detail.actNum);
                xmlTextWriter.writeElementString("DrwrRemrk", query.detail.memo+splitToString+query.detail.reqSeqID);
                xmlTextWriter.writeStartElement("DraftInfo");
                xmlTextWriter.writeStartElement("Map");
                //电子票据号码
                xmlTextWriter.writeElementString("draftNb", query.detail.billNo);
                //票据金额
                xmlTextWriter.writeElementString("isseAmt", query.detail.billAmt);
                xmlTextWriter.writeEndDocument();
                // 发送给银行获得应答报文
                String[] result = sendToBank(xmlTextWriter.toXmlString(), bankHeader);

                if (result[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String draftNb = tmpBkReader.readedXElement.getSingleText("draftNb");
                                if (StringHelper.hasAnyChar(draftNb) && query.detail.billNo.equals(draftNb)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                    detail.reqSeqID = query.detail.reqSeqID;
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                    detail.payInfoCode = detail.transState;
                                    break;
                                } else {
                                    detail.respCode = "-1";
                                    detail.respInfo = "未返回数据";
                                }
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();

        return retAts;
    }

    //背书转让申请 ok
    @Override
    public RetAtsB105BillEndoSubmit D_B105_BillResp_Submit(ReqB105BillEndoSubmit query) throws Exception {
        RetAtsB105BillEndoSubmit retAts = new RetAtsB105BillEndoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EEndorsementApply";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
            RetAtsB105BillEndoSubmitDetail detail = new RetAtsB105BillEndoSubmitDetail();
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入SDEBANK01通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            //写入发送给银行报文体
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("BanEndrsmtMk", getTransFlag(query.detail.transFlag));//背书不得转让标记
            xmlWriter.writeElementString("EndrsrRemrk", query.detail.memo+splitToString+query.detail.reqSeqID);//背书人备注
            xmlWriter.writeElementString("EnterpriseNm", query.detail.endoName);//背书人名称
            xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//背书人账号
            xmlWriter.writeElementString("EndrsAcctSvcr", query.detail.endoBnkCode);//背书人开户行行号
            xmlWriter.writeElementString("EndrseeNm", query.detail.receName);//被背书人名称
            xmlWriter.writeElementString("EndrseeAcctId", query.detail.receAct);//被背书人账号
            xmlWriter.writeElementString("EndrseeAcctSvcr", query.detail.receBnkCode);//被背书人开户行行号
            xmlWriter.writeElementString("EndrseeAcctSvcrName", query.detail.receBnkName);//被背书人开户行名称
            xmlWriter.writeStartElement("DraftInfo");
            xmlWriter.writeStartElement("Map");
            xmlWriter.writeElementString("draftNb", query.detail.billNo);//电子票据号码
            xmlWriter.writeElementString("isseAmt", query.detail.billAmt);//电子票据金额
            xmlWriter.writeEndDocument();

            // 发送给银行获得应答报文
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);

            if (tmpStrRet[0].length() == 0) {// 成功响应
                StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                if (tmpBkReader != null) {// 返回报文的内容不为空
                    String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                    if ("000000".equals(IBSReturnCode)) {
                        while (tmpBkReader.read("/Message/Body/List/Map")) {
                            String draftNb = tmpBkReader.readedXElement.getSingleText("draftNb");
                            if (StringHelper.hasAnyChar(draftNb) && query.detail.billNo.equals(draftNb)) {
                                detail.respCode = "0";
                                detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                detail.reqSeqID = query.detail.reqSeqID;
                                detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                detail.payInfoCode = detail.transState;
                                break;
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = "未返回数据";
                            }
                        }
                    } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                        detail.respCode = "0";
                        detail.respInfo = IBSReturnMsg;
                        detail.transState = IBSReturnCode;
                        detail.payInfoCode = detail.transState;
                        detail.reqSeqID=query.detail.reqSeqID;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpStrRet[1];
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //贴现 ok
    @Override
    public RetAtsB106BillDiscSubmit D_B106_BillResp_Submit(ReqB106BillDiscSubmit query) throws Exception {
        RetAtsB106BillDiscSubmit retAts = new RetAtsB106BillDiscSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EDiscountApply";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            RetAtsB106BillDiscSubmitDetail detail = new RetAtsB106BillDiscSubmitDetail();
            try {
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("RpdMk", getDiscType(query.detail.discType));//贴现种类 RM00买断式 RM01回购式
                xmlWriter.writeElementString("IntrstRateYear", query.detail.discRate);//贴现利率
                xmlWriter.writeElementString("BanEndrsmtMk", getTransFlag(query.detail.transFlag));//背书不得转让标记
                xmlWriter.writeElementString("SttlmMk", getPayType(query.detail.payType));//清算标记
                xmlWriter.writeElementString("RpdOpenDt", DateHelper.convertDateTimetoD8(query.detail.repOpenDate));//贴现赎回开放日
                xmlWriter.writeElementString("RpdDueDt", DateHelper.convertDateTimetoD8(query.detail.repDueDate));//贴现赎回截止日
                xmlWriter.writeElementString("RpdIntrstRate", query.detail.repRate);//贴现赎回利率
                xmlWriter.writeElementString("BkAmt3", query.detail.repAmt);//贴现赎回金额
                xmlWriter.writeElementString("ContractNb", query.detail.contractNo);//贴现交易合同编号
                xmlWriter.writeElementString("InvoiceNb", query.detail.invoiceNo);//贴现发票号码
                //xmlWriter.writeElementString("DscntPropsrRemrk",getBankConfig().getMapValueByKey("busDeptIdMapping",query.detail.actNum));
                xmlWriter.writeElementString("DscntPropsrRemrk", query.detail.memo+splitToString+query.detail.reqSeqID);//贴出人备注
                xmlWriter.writeElementString("AccnInfId", query.detail.actedAct);//入账帐号
                xmlWriter.writeElementString("AccnInfAcctSvcr", query.detail.actedBnkCode);//入帐开户行
                xmlWriter.writeElementString("AccnInfAcctSvcrName", query.detail.actedBnkName);//入帐开户行名
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//贴现申请人账号
                xmlWriter.writeElementString("DrwrNm", query.detail.discName);//贴现申请人名称
                xmlWriter.writeElementString("DscntBkNm", query.detail.receName);//贴入人名称
                xmlWriter.writeElementString("DscntBkId", query.detail.receAct);//贴入人账号
                xmlWriter.writeElementString("DscntBkAcctSvcr", query.detail.receBnkCode);//贴入人行号
                xmlWriter.writeElementString("DscntBkAcctSvcrName", query.detail.receBnkName);//贴入人行名
                xmlWriter.writeElementString("BusDeptId", getBankConfig().getMapValueByKey("busDeptIdMapping",query.detail.actNum));//业务经办支行 TODO
                xmlWriter.writeStartElement("DraftInfo");
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("draftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("isseAmt", query.detail.billAmt);//电子票据金额
                xmlWriter.writeElementString("dueDt", DateHelper.convertDateTimetoD8(query.detail.dueDate));//汇票到期日
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String draftNb = tmpBkReader.readedXElement.getSingleText("draftNb");
                                if (StringHelper.hasAnyChar(draftNb) && query.detail.billNo.equals(draftNb)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                    detail.reqSeqID = query.detail.reqSeqID;
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                    detail.payInfoCode = detail.transState;
                                    break;
                                } else {
                                    detail.respCode = "-1";
                                    detail.respInfo = "未返回数据";
                                }
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //质押 ok
    @Override
    public RetAtsB107BillPledSubmit D_B107_BillResp_Submit(ReqB107BillPledSubmit query) throws Exception {
        RetAtsB107BillPledSubmit retAts = new RetAtsB107BillPledSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EImpawnApply";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            RetAtsB107BillPledSubmitDetail detail = new RetAtsB107BillPledSubmitDetail();
            try {
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("RmrkByPropsr", query.detail.memo+splitToString+query.detail.reqSeqID);//出质人备注
                xmlWriter.writeElementString("EnterpriseNm", query.detail.pledName);//出质人名称
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//出质人账号
                xmlWriter.writeElementString("CollztnBkNm", query.detail.receName);//质权人名称
                xmlWriter.writeElementString("CollztnBkAcct", query.detail.receAct);//质权人账号
                xmlWriter.writeElementString("CollztnBkAcctSvcr", query.detail.receBnkCode);//质权人开户行行号
                xmlWriter.writeElementString("CollztnBkAcctSvcrName", query.detail.receBnkName);//质权人开户行名
                xmlWriter.writeStartElement("DraftInfo");
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("draftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("isseAmt", query.detail.billAmt);//电子票据金额
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String draftNb = tmpBkReader.readedXElement.getSingleText("draftNb");
                                if (StringHelper.hasAnyChar(draftNb) && query.detail.billNo.equals(draftNb)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                    detail.reqSeqID = query.detail.reqSeqID;
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                    detail.payInfoCode = detail.transState;
                                    break;
                                } else {
                                    detail.respCode = "-1";
                                    detail.respInfo = "未返回数据";
                                }
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //提示付款 ok
    @Override
    public RetAtsB108BillPaySubmit D_B108_BillResp_Submit(ReqB108BillPaySubmit query) throws Exception {
        RetAtsB108BillPaySubmit retAts = new RetAtsB108BillPaySubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EPaymentApply";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            RetAtsB108BillPaySubmitDetail detail = new RetAtsB108BillPaySubmitDetail();
            try {
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//签约电票的账号

                //测试 TODO
                query.detail.actName="测试账户";

                xmlWriter.writeElementString("EnterpriseNm", StringHelper.hasAnyChar(query.detail.actName)
                        ?query.detail.actName:query.detail.promptPayName);//提示付款人名称
                xmlWriter.writeElementString("DrftHldRsn", query.detail.overDueRea);//逾期原因说明
                xmlWriter.writeElementString("SttlmStyle", "1".equals(query.detail.payType) ? "0" : "1");//清算方式
                xmlWriter.writeElementString("DrftHldRemrk", query.detail.memo+splitToString+query.detail.reqSeqID);//提示付款人备注
                xmlWriter.writeElementString("BkBrchNo2", query.detail.promptPayBnkCode);//提示付款人承接行行号
                xmlWriter.writeStartElement("DraftInfo");
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("draftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("isseAmt", query.detail.billAmt);//电子票据金额
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String draftNb = tmpBkReader.readedXElement.getSingleText("draftNb");
                                if (StringHelper.hasAnyChar(draftNb) && query.detail.billNo.equals(draftNb)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                    detail.reqSeqID = query.detail.reqSeqID;
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                    detail.payInfoCode = detail.transState;
                                    break;
                                } else {
                                    detail.respCode = "-1";
                                    detail.respInfo = "未返回数据";
                                }
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //提示保证 未测
    @Override
    public RetAtsB109BillGuarSubmit D_B109_BillResp_Submit(ReqB109BillGuarSubmit query) throws Exception {
        RetAtsB109BillGuarSubmit retAts = new RetAtsB109BillGuarSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EGuaranteeApply";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            RetAtsB109BillGuarSubmitDetail detail = new RetAtsB109BillGuarSubmitDetail();
            try {
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                if ("1".equals(query.detail.guarType)) {
                    xmlWriter.writeElementString("GuarnteeTp", "0");//保证类别
                } else if ("2".equals(query.detail.guarType)) {
                    xmlWriter.writeElementString("GuarnteeTp", "1");//保证类别
                } else if ("3".equals(query.detail.guarType)) {
                    xmlWriter.writeElementString("GuarnteeTp", "2");//保证类别
                }
                xmlWriter.writeElementString("GuarnteeByremark", query.detail.memo+splitToString+query.detail.reqSeqID);//被保证人备注
                xmlWriter.writeElementString("GuarnteeByType", "RC01");//被保证人类别
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//被保证人账号
                xmlWriter.writeElementString("GuarnteeName", query.detail.receName);//保证人名称
                xmlWriter.writeElementString("GuarnteeNo", query.detail.receAct);//保证人账号
                xmlWriter.writeElementString("GuarntrAcctSvcr", query.detail.receBnkCode);//保证人开户行行号
                xmlWriter.writeElementString("GuarntrAcctSvcrName", query.detail.receBnkName);//保证人开户行名称

                xmlWriter.writeStartElement("DraftInfo");
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("draftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("isseAmt", query.detail.billAmt);//电子票据金额
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String draftNb = tmpBkReader.readedXElement.getSingleText("draftNb");
                                if (StringHelper.hasAnyChar(draftNb) && query.detail.billNo.equals(draftNb)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                    detail.reqSeqID = query.detail.reqSeqID;
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                    detail.payInfoCode = detail.transState;
                                    break;
                                } else {
                                    detail.respCode = "-1";
                                    detail.respInfo = "未返回数据";
                                }
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //追索通知 未测
    @Override
    public RetAtsB110BillRecoSubmit D_B110_BillResp_Submit(ReqB110BillRecoSubmit query) throws Exception {
        RetAtsB110BillRecoSubmit retAts = new RetAtsB110BillRecoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2ERecourseApply";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            RetAtsB110BillRecoSubmitDetail detail = new RetAtsB110BillRecoSubmitDetail();
            try {
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("DraftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("IsseAmt", query.detail.billAmt);//票据金额
                xmlWriter.writeElementString("RcrsTp", getRecourseType(query.detail.recourseType));//追索类型
                xmlWriter.writeElementString("ApplDt", DateHelper.convertDateTimetoD8(query.reqHeader.payDateTime));//追索通知日期
                xmlWriter.writeElementString("ReqAmt", query.detail.recourseAmt);//追索金额
                xmlWriter.writeElementString("RcrsRsnCd", "1".equals(query.detail.recourseCode) ? "RC00" : "RC01");//追索理由代码
                xmlWriter.writeElementString("Remrk", query.detail.memo+splitToString+query.detail.reqSeqID);//追索通知备注
                xmlWriter.writeElementString("EnterpriseNm", query.detail.recoName);//追索人名称
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//追索人账号
                xmlWriter.writeElementString("RcvgCmonId", query.detail.receCode);//被追索人组织机构代码
                xmlWriter.writeElementString("RcvgNm", query.detail.receName);//被追索人名称
                xmlWriter.writeElementString("RcvgAcctId", query.detail.receAct);//被追索人账号
                xmlWriter.writeElementString("RcvgAcctSvcr", query.detail.receBnkCode);//被追索人开户行行号
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/Message/Body/ReturnMsg");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/Message/Body/ReturnCode");
                            detail.payInfoCode = detail.transState;
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //同意清偿 未测
    @Override
    public RetAtsB111BillAgreeSubmit D_B111_BillResp_Submit(ReqB111BillAgreeSubmit query) throws Exception {
        RetAtsB111BillAgreeSubmit retAts = new RetAtsB111BillAgreeSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2ERecourseAgreement";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            RetAtsB111BillAgreeSubmitDetail detail = new RetAtsB111BillAgreeSubmitDetail();
            try {
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("DraftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("IsseAmt", query.detail.billAmt);//票据金额
                xmlWriter.writeElementString("ApplDt", DateHelper.convertDateTimetoD8(query.reqHeader.payDateTime));//同意清偿日期
                xmlWriter.writeElementString("ReqAmt", query.detail.agreeAmt);//同意清偿金额
                xmlWriter.writeElementString("Remrk", query.detail.memo+splitToString+query.detail.reqSeqID);//同意清偿人备注
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//同意清偿人信息账号
                String[] ecdsInfo = query.detail.busiNo.split("_");
                if (StringHelper.hasAnyChar(query.detail.busiNo)) {
                    //                20 BkOldPlatDate 原ECDS平台日期 Char(8) F 　
                    xmlWriter.writeElementString("EcdsDate", ecdsInfo[0]);
//                21 BkOldSeq 原ECDS平台流水 Int F 　
                    xmlWriter.writeElementString("EcdsSeq", ecdsInfo[1]);
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "原ECDS平台流水异常。请确认申请结果查询是否返回EcdsDate和EcdsSeq的值";
                    WriteBankLogLn("原ECDS平台流水异常。请确认申请结果查询是否返回EcdsDate和EcdsSeq的值");
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/Message/Body/ReturnMsg");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/Message/Body/ReturnCode");
                            detail.payInfoCode = detail.transState;
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    //交易撤回 ok
    @Override
    public RetAtsB112BillCancSubmit D_B112_BillResp_Submit(ReqB112BillCancSubmit query) throws Exception {
        RetAtsB112BillCancSubmit retAts = new RetAtsB112BillCancSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EAcceptCancel";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        while (query.readRecord()) {
            RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();
            try {
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                //    撤回类型 1-承兑申请撤回 2-提示收票申请撤回 3-背书转让申请撤回 4-贴现申请撤回 5-质押申请撤回 6-（逾期）提示付款撤回
//    7-出票保证申请撤回 8-承兑保证申请撤回  9-背书保证申请撤回 10-追索通知撤回 11-同意清偿撤回 12-解质押申请撤回
                String bkType;
                switch (Integer.valueOf(query.detail.cancelType)) {
                    case 1: bkType = "0001";break;
                    case 2: bkType = "0002";break;
                    case 3: bkType = "0003";break;
                    case 4: bkType = "0004";break;
                    case 5: bkType = "0005";break;
                    case 6: bkType = "0007";break;
                    case 7: bkType = "0016";break;
                    case 8: bkType = "0017";break;
                    case 9: bkType = "0018";break;
                    case 10: bkType = "0008";break;
                    case 11: bkType = "0009";break;
                    case 12: bkType = "0006";break;
                    default: throw new Exception("未知的撤销类型：" + query.detail.cancelType);
                }
                xmlWriter.writeElementString("CancelType", bkType);//撤销类型
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//撤销申请人账号
                xmlWriter.writeStartElement("DraftInfo");
                xmlWriter.writeStartElement("Map");
                //电子票据号码
                xmlWriter.writeElementString("draftNb", query.detail.billNo);
                //票据金额
                xmlWriter.writeElementString("isseAmt", query.detail.billAmt);
                String[] ecdsInfo = query.detail.busiNo.split("_");
                if(ecdsInfo.length==2){
                    xmlWriter.writeElementString("EcdsDate", ecdsInfo[0]);//原ECDS平台日期
                    xmlWriter.writeElementString("EcdsSeq", ecdsInfo[1]);//原ECDS平台流水号
                }else {
                    xmlWriter.writeElementString("EcdsDate", "");//原ECDS平台日期
                    xmlWriter.writeElementString("EcdsSeq", "");//原ECDS平台流水号
                }
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                                if (StringHelper.hasAnyChar(billNo) && billNo.equals(query.detail.billNo)) {
                                    detail.respCode = "0";
                                    detail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                    detail.reqSeqID = query.detail.reqSeqID;
                                    detail.transState = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                    detail.payInfoCode = detail.transState;
                                    break;
                                } else {
                                    detail.respCode = "-1";
                                    detail.respInfo = "查无记录";
                                }
                            }
                        } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {// 如果是ct通讯机校验错误，或者通讯失败
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.transState = IBSReturnCode;
                            detail.payInfoCode = detail.transState;
                            detail.reqSeqID=query.detail.reqSeqID;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = "银企直联通讯机校验错误，或者通讯失败！";
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }


    @Override
    public RetAtsB113ProtocolSubmit D_B113_ProtocolResp_Submit(ReqB113ProtocolSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB119BillRelePledSubmit D_B119_BillResp_Submit(ReqB119BillRelePledSubmit query) throws Exception {
        return null;
    }
}
